<?php

class LoginForm extends sfForm {

    public function configure() {
        $this->setWidgets(array(
            'login' => new sfWidgetFormInput(),
            'password' => new sfWidgetFormInputPassword()
        ));

        $this->widgetSchema->setNameFormat('login[%s]');

        $this->widgetSchema->setLabels(array(
            'login' => "Login",
            'password' => "Password"
        ));

        $this->setValidators(array(
            'login' => new sfValidatorAnd(
                    array(
                        new sfValidatorString(
                                array('required' => true, 'min_length' => 3, 'max_length' => 14),
                                array(
                                    'min_length' => "The login is too short. 3 characters minimum.",
                                    'max_length' => "The login is too long. 14 characters maximum",
                                )
                        ),
                        new sfValidatorRegex(
                                array('pattern' => '/^[a-zA-Z0-9-]+$/')
                        )
                    ),
                    array(),
                    array(
                        'required' => "The login is required",
                        'invalid' => "The nickname could not contain special characters."
                    )
            ),
            'password' => new sfValidatorString(
                    array('required' => true, 'min_length' => 6, 'max_length' => 20),
                    array(
                        'min_length' => "The password is too short. 6 characters minimum.",
                        'max_length' => "The password is too long. 20 characters maximum",
                        'required' => "Password is required",
                        'invalid' => "The password must contains between 6 and 20 characters"
                    )
            ),
        ));

        $this->validatorSchema->setPostValidator(
                new sfValidatorCallback(array('callback' => array($this, 'checkLogin')))
        );
    }

    public function checkLogin($validator, $values) {
        $login = $values['login'];
        $password = $values['password'];
        if (!empty($login) && !empty($password)) {
            $person = Doctrine::getTable('Person')->findOneBy('login', $login);
            if ($person != null) {
                if ($person->getPassword() == md5($password)) {
                    // Login correct !
                    if ($person->getStatus() == 1) {
                        return $values;
                    } else if ($person->getStatus() == 0) {
                        throw new sfValidatorError($validator, 'Your account is still disabled. Please click on the confirmation email to activate your account.');
                    } else {
                        throw new sfValidatorError($validator, 'You are bannished from the website.');
                    }
                } else {
                    // Connexion incorrecte
                    throw new sfValidatorError($validator, 'Log in incorrect'.md5($password));
                }
            } else {
                throw new sfValidatorError($validator, 'This login does nt exists');
            }
        }
    }

}